package cn.virens.dd.execute;

import java.util.Queue;

import cn.virens.dd.Strategy;
import cn.virens.dd.StrategyTask;
import cn.virens.dd.StrategyValue;

public class ExecuteWorkStrategyTasks extends ExecuteWork<ExecuteWorkStrategyTasks.StrategyWarp, StrategyTask> {
	public ExecuteWorkStrategyTasks(Integer queueSize, Integer queueMax) {
		super(queueSize, queueMax);
	}

	@Override
	protected ExecuteResult execute0(StrategyWarp warp, Queue<StrategyTask> queue) {
		ExecuteResult result = new ExecuteResult();

		StrategyTask queueTask = null;

		// 将当前策略下的所有task全部都执行一遍,如果抛了异常需要存到返回集中
		while ((queueTask = queue.poll()) != null) {
			result.execute(queueTask, (task) -> {
				task.execute(warp.strategy, warp.value);
			});
		}

		return result;
	}

	protected StrategyWarp warp(Strategy strategy, StrategyValue value) {
		return new StrategyWarp(strategy, value);
	}

	protected static class StrategyWarp {
		private Strategy strategy;
		private StrategyValue value;

		private StrategyWarp(Strategy strategy, StrategyValue value) {
			this.strategy = strategy;
			this.value = value;
		}

	}
}
